/*
 * @Author: szx
 * @Date: 2021-12-05 14:26:33
 * @LastEditTime: 2021-12-05 14:45:52
 * @Description:
 * @FilePath: \leetcode\100-199\160\160.js
 */
/**
 * @param {ListNode} headA
 * @param {ListNode} headB
 * @return {ListNode}
 */
var getIntersectionNode = function (headA, headB) {
    if (!headA || !headB) return null;
    let curA = headA,
        curB = headB;
    while (curA != curB) {
        curA = curA ? curA.next : headB;
        curB = curB ? curB.next : headA;
    }
    return curA;
};

var getIntersectionNode = function (headA, headB) {
    let a = headA,
        b = headB;
    let la = 0,
        lb = 0;
    while (a) {
        la++;
        a = a.next;
    }
    while (b) {
        lb++;
        b = b.next;
    }
    if (lb > la) {
        let tmp = headA;
        headA = headB;
        headB = tmp;
        tmp = la;
        la = lb;
        lb = tmp;
    }
    (a = headA), (b = headB);
    let len = la - lb;
    while (len--) {
        a = a.next;
    }
    while (a != b) {
        a = a.next;
        b = b.next;
    }
    return a;
};
